package com.zr.inquiry.total.service.impl;

import com.zr.inquiry.spare.model.pojo.InquirySpareDetailsVo;
import com.zr.inquiry.total.service.QuotationService;
import com.zr.share.config.ErrorMassage;
import com.zr.share.enums.InquiryCurrencyEnum;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName QuotationServiceImpl
 * @Description TODO
 * @Author 马家奎
 * @Date 2019/11/19 13:12
 */
@Service
public class QuotationServiceImpl implements QuotationService {

    @Override
    public ResultUtil importFile(MultipartFile file) {
        //2.提示版本问题，询问产品经理支持哪种版本的导入格式，使用2007版本的导入模板，根据版本不同，poi导入技术的读取方式不同
        if (!file.getOriginalFilename().contains("xlsx")){
            return ResultBuilderUtil.error(ErrorMassage.FILE_TYPE_NOT_SUPPORT);
        }
        //验证模板、字段值是否正确
        ResultUtil<List<InquirySpareDetailsVo>> resultFile = poiReadFile(file);
        if (!resultFile.getSuccess()){
            return resultFile;
        }
        ResultUtil validateImport = validateImport(resultFile.getData());
        if (validateImport.getSuccess()) {
            return validateImport;
        }
        return validateImport;
    }
    private ResultUtil<List<InquirySpareDetailsVo>> poiReadFile(MultipartFile file) {
        List<InquirySpareDetailsVo> inquirySpareList = new ArrayList<>();
        //poi核心类，用来读取excel表格中的数据
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            //获取单元格中的信息 at0是获取sheet1中的数据。
            XSSFSheet sheet = workbook.getSheetAt(0);
            //验证第一列第一行的表格标头信息是否为 “询价单导入”，如果不是，提示模板错误。
            if (!String.valueOf(sheet.getRow(sheet.getFirstRowNum()).getCell(0)).equals(ErrorMassage.INQUIRY_TEMPLATE)) {
                return ResultBuilderUtil.error(ErrorMassage.TEMPLATE_ERROR);
            }
            //遍历excel表格中的所有数据，从第五行开始读取，没有数据时终止遍历。
            for (int i = sheet.getFirstRowNum() + 4; i <= sheet.getLastRowNum(); i++) {
                //读取每一行的数据
                XSSFRow xssfRow = sheet.getRow(i);
                if(xssfRow!=null) {
                    if (!String.valueOf(xssfRow.getCell(1)).equals("")
                            ||!String.valueOf(xssfRow.getCell(2)).equals("")
                            ||!String.valueOf(xssfRow.getCell(3)).equals("")){
                        InquirySpareDetailsVo inquirySpare = new InquirySpareDetailsVo();
                        inquirySpare.setSpareCoding(String.valueOf(xssfRow.getCell(1)));
                        inquirySpare.setSpareName(String.valueOf(xssfRow.getCell(2)));
                        inquirySpare.setMeasurementUnit(String.valueOf(xssfRow.getCell(3)));
                        inquirySpare.setInquiryQty(Integer.parseInt(String.valueOf(xssfRow.getCell(4))));
                        inquirySpare.setCurrencyCodeName(String.valueOf(xssfRow.getCell(5)));
                        inquirySpare.setDeliveryCycle(Integer.parseInt(String.valueOf(xssfRow.getCell(6))));
                        inquirySpareList.add(inquirySpare);
                    }
                }
            }
        } catch (IOException e) {
            return ResultBuilderUtil.error(ErrorMassage.FILE_NOT_BE_FOUND);
        }catch (ClassCastException c){
            return ResultBuilderUtil.error(ErrorMassage.TEXT_FORMAT_ERROR);
        }catch (NullPointerException n){
            return ResultBuilderUtil.error(ErrorMassage.TEXT_FORMAT_ERROR);

        }
        return ResultBuilderUtil.success(inquirySpareList);
    }

    public ResultUtil validateImport( List<InquirySpareDetailsVo> inquirySpareDetailVoList){
        //5.验证不为空字段是否为空
        //6.验证字符长度是否符合需求
        //7.验证数据是否存在也就是是否合法
        //8.验证数据是否重复
        // 9.对于需要转换的数据进行数据转换，比如数据库存的数据为数字格式，用户输入的为汉字，需要把汉字转换为数字存储
        for (InquirySpareDetailsVo inquirySpareCodeVo:inquirySpareDetailVoList){
            //验证备件编码
            if (inquirySpareCodeVo.getSpareCoding().isEmpty()){
                return ResultBuilderUtil.error(ErrorMassage.SPARE_CODE_NOT_NULL);
            }else{
                if (inquirySpareCodeVo.getSpareCoding().length()>50){
                    return ResultBuilderUtil.error(ErrorMassage.SPARE_CODE_TOO_LONG);
                }
            }
            //验证备件名称
            if (inquirySpareCodeVo.getSpareName().isEmpty()){
                return ResultBuilderUtil.error(ErrorMassage.SPARE_NAME_NOT_NULL);
            }else{
                if (inquirySpareCodeVo.getSpareName().length()>50){
                    return ResultBuilderUtil.error(ErrorMassage.SPARE_NAME_TOO_LONG);
                }
            }
            //验证币别
            if(InquiryCurrencyEnum.getIndex(inquirySpareCodeVo.getCurrencyCodeName())==null){
                return ResultBuilderUtil.error(ErrorMassage.INQUIRY_CURRENCY_NOT_EXIST);
            }else{
                inquirySpareCodeVo.setCurrencyCode(InquiryCurrencyEnum.getIndex(inquirySpareCodeVo.getCurrencyCodeName()));
            }
        }
        return ResultBuilderUtil.success(inquirySpareDetailVoList);
    }
}
